package com.nice.nice201110.service;

import com.nice.nice201110.entity.TBaseCapital;
import com.nice.nice201110.entity.TLog;
import com.nice.nice201110.mapper.TBaseCapitalMapper;
import com.nice.nice201110.mapper.TLogMapper;
import com.nice.nice201110.utils.NiceTableEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;


@Service
public class TbaseCapitalService {

    @Autowired
    TBaseCapitalMapper tBaseCapitalMapper;

    @Autowired
    TLogMapper tLogMapper;

    @Autowired
    private PlatformTransactionManager transactionManager;

    private TLog tLog = new TLog();

    /**  查询数据 */
    public List<TBaseCapital> getlist(BaseVo baseVo){
        return tBaseCapitalMapper.selectByPrimaryKeys(baseVo);
    }
    /** 查询单一数据 */
    /**
     *      *  TransactionDefinition.ISOLATION_DEFAULT：这是默认值，表示使用底层数据库的默认隔离级别。对大部分数据库而言，通常这值就是TransactionDefinition.ISOLATION_READ_COMMITTED。
     *      *  TransactionDefinition.ISOLATION_READ_UNCOMMITTED：该隔离级别表示一个事务可以读取另一个事务修改但还没有提交的数据。该级别不能防止脏读，不可重复读和幻读，因此很少使用该隔离级别。比如PostgreSQL实际上并没有此级别。
     *      *  TransactionDefinition.ISOLATION_READ_COMMITTED：该隔离级别表示一个事务只能读取另一个事务已经提交的数据。该级别可以防止脏读，这也是大多数情况下的推荐值。
     *      *  TransactionDefinition.ISOLATION_REPEATABLE_READ：该隔离级别表示一个事务在整个过程中可以多次重复执行某个查询，并且每次返回的记录都相同。该级别可以防止脏读和不可重复读。
     *      *  TransactionDefinition.ISOLATION_SERIALIZABLE：所有的事务依次逐个执行，这样事务之间就完全不可能产生干扰，也就是说，该级别可以防止脏读、不可重复读以及幻读。但是这将严重影响程序的性能。通常情况下也不会用到该级别。
     *
     *  rollbackFor = Exception.class  自动回滚
     *  //强制手动事务回滚  TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
     * */
    public TBaseCapital getOne(UserVo userVo, Integer id){
        TBaseCapital tBaseCapital = tBaseCapitalMapper.selectByPrimaryKey(id);
        if(tBaseCapital!=null){
            tLog.setDate(new Date());
            tLog.setType(3);  //操作类型（新增0，修改1，删除2,查询3）
            tLog.setDocid(tBaseCapital.getId());
            tLog.setUsername(userVo.getName());
            tLog.setUserid(Integer.valueOf(userVo.getId()));
            tLog.setTtable(NiceTableEnum.form_base_capital);
            tLog.setRemark("查看资产:"+tBaseCapital.toString());
            tLogMapper.insertSelective(tLog);
        }
        return tBaseCapital;
    }
    /** 更新资产数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean updateTbaseCapital(UserVo userVo,TBaseCapital tBaseCapital){
        try {
            int num = tBaseCapitalMapper.updateByPrimaryKeySelective(tBaseCapital);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(1);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tBaseCapital.getId()); //  数据id
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_base_capital);
                tLog.setRemark("更新资产:"+tBaseCapital.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 插入资产数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean inserttBaseCapital(UserVo userVo,TBaseCapital tBaseCapital){
        try {
            int num = tBaseCapitalMapper.insertSelective(tBaseCapital);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(0);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tBaseCapital.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_base_capital);
                tLog.setRemark("新增资产:"+tBaseCapital.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 删除数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean deletetBaseCapital(UserVo userVo,Integer id){
        try {
            int num = tBaseCapitalMapper.delSelective(id);
            if (num > 0) {
                tLog.setDate(new Date());
                tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(id); //  数据id
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_base_capital);
                tLog.setRemark("删除资产:" + tBaseCapitalMapper.selectByPrimaryKey(id).toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 删除数据s */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean deletestBaseCapital(UserVo userVo,String ids){
        String StrArr [] = ids.split(",");
        try {
            for(String strID:StrArr) {
                if(strID!=null && strID.trim().length()>0) {
                    int id = Integer.valueOf(strID);
                    int num = tBaseCapitalMapper.delSelective(id);
                    if (num > 0) {
                        tLog.setDate(new Date());
                        tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                        tLog.setDocid(id); //  数据id
                        tLog.setUsername(userVo.getName());
                        tLog.setUserid(Integer.valueOf(userVo.getId()));
                        tLog.setTtable(NiceTableEnum.form_base_capital);
                        tLog.setRemark("删除资产:" + tBaseCapitalMapper.selectByPrimaryKey(id).toString());
                        tLogMapper.insertSelective(tLog);
                    }else{
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }


}
